package cn.laniakea.solrj;

import cn.laniakea.solrj.pojo.Item;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.impl.HttpSolrServer;
import org.apache.solr.common.SolrInputDocument;
import org.junit.Test;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class SolrjCreate {
    /**
     * 新增solrServer.add(doc);
     * @throws IOException
     * @throws SolrServerException
     */
    @Test
    public void testSolrjCreate() throws IOException, SolrServerException {
        //创建sorl服务
        HttpSolrServer solrServer = new HttpSolrServer("http://localhost:8080/solr/core1");
        SolrInputDocument doc=new SolrInputDocument();
        doc.addField("id","7");
        doc.addField("title","java新特性");
        solrServer.add(doc);
        solrServer.commit();
    }

    /**
     * 新增 solrServer.addBean(item);
     * @throws IOException
     * @throws SolrServerException
     */
    @Test
    public void testSolrjCreateBean() throws IOException, SolrServerException {
        //创建sorl服务
        HttpSolrServer solrServer = new HttpSolrServer("http://localhost:8080/solr/core1");
        /*SolrInputDocument doc=new SolrInputDocument();
        doc.addField("id","7");
        doc.addField("title","java新特性");*/
        Item item = new Item();
        item.setId("8");
        item.setTitle("lamda表达式");
        item.setPrice(999F);
        solrServer.addBean(item);
        solrServer.commit();
    }

    /**
     * 新增beans
     * @throws IOException
     * @throws SolrServerException
     */
    @Test
    public void testSolrjCreateBeans() throws IOException, SolrServerException {
        //创建sorl服务
        HttpSolrServer solrServer = new HttpSolrServer("http://localhost:8080/solr/core1");
        /*SolrInputDocument doc=new SolrInputDocument();
        doc.addField("id","7");
        doc.addField("title","java新特性");*/
        List<Item> itemList=new ArrayList<>();
        for (int i = 0; i < 5; i++) {
            Item item = new Item();
            item.setId("9"+i);
            item.setTitle("先锋"+i+"代");
            item.setPrice(999F+i*100);
            itemList.add(item);
        }

        solrServer.addBeans(itemList);
        solrServer.commit();
    }
    /**
     * 修改solrServer.add(doc);
     * @throws IOException
     * @throws SolrServerException
     */
    @Test
    public void testSolrjCreateUpdate() throws IOException, SolrServerException {
        //创建sorl服务
        HttpSolrServer solrServer = new HttpSolrServer("http://localhost:8080/solr/core1");
        SolrInputDocument doc=new SolrInputDocument();
        doc.addField("id","7");
        doc.addField("title","java新特性更新版");
        doc.addField("price",777F);
        solrServer.add(doc);
        solrServer.commit();
    }

}
